package com.zzh.handler;

import com.zzh.entity.Result;
import com.zzh.enums.ResultEnum;
import com.zzh.exception.MyException;
import com.zzh.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 异常统一处理类，方便用户可以更加友好的看到错误信息
 * @author wenrouyue
 * @ time: 2019/11/9
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 处理系统内部异常
     * @param e e
     * @return e
     */
    @ExceptionHandler(Exception.class)
    public Result<Object> handleException(Exception e){
        e.printStackTrace();
        log.info("handleException_异常:{}", e.getMessage());
        return ResultUtil.getError(ResultEnum.SystemException.getCode(),
                ResultEnum.ServiceException.getMsg());
    }

    /**
     * 处理自定义异常
     * @param e e
     * @return e
     */
    @ExceptionHandler(MyException.class)
    public Result<Object> handleMyException(MyException e){
        e.printStackTrace();
        log.info("handleMyException_自定义异常:{}", e.getMessage());
        return ResultUtil.getError(e.getCode(), e.getMessage());
    }

}
